<template>
    <header>
        <h2>闪购商城</h2>
            <router-link to="/index/main" class="nav-btn">首页</router-link>
            <router-link to="/index/cart" class="nav-btn">购物车</router-link>
            <p style="margin: 10px;">欢迎，{{username}}</p>
            <button type="danger" @click="logout">退出登录</button>
    </header>
</template>

<script setup name="Header">
import { ref } from 'vue'

const username = ref(localStorage.getItem('UserName') || '游客')

const logout = () => {
  localStorage.removeItem('UserName')
  location.href = '/login'
}

</script>

<style>
    header {
        padding: 10px 50px;
        margin-bottom: 2px;
        box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4);
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    h2 {
        color: rgb(251, 60, 60);
        font-weight: 700;
        margin-right: auto;
    }
    .nav-btn {
        padding: 5px 20px;
        margin: 0 5px;
        font-weight: 600;
        border: none;
        border-radius: 4px;
        text-decoration: none;
        cursor: pointer;
        transition: background-color 0.2s;
    }
    .nav-btn:not(.router-link-active) {
        background-color: #f5f5f5;
        color: #666;
    }
    .router-link-active {
        background-color: rgb(252, 25, 25);
        color: #fff;
    }
    button {
        padding: 8px 20px;
        margin: 0px 5px;
        font-weight: 600;
        border: none;
        border-radius: 4px;
        background-color: rgb(252, 25, 25);
        color: #fff;
        cursor: pointer;
    }
    button:hover {
        background-color: rgb(255, 102, 102);
    }
    button:active {
        background-color: rgb(255, 0, 0);
    }
    p {
        margin: 0;
        font-size: 14px;
        color: rgb(251, 39, 39);
        font-weight: 600;
    }
</style>